package cn.seimes.zjcc.service.impl;

import cn.seimes.result.JsonResult;
import cn.seimes.zjcc.domain.User;
import cn.seimes.zjcc.dto.MovTicDto;
import cn.seimes.zjcc.feign.AllMoveTickeetService;
import cn.seimes.zjcc.mapper.UserMapper;
import cn.seimes.zjcc.service.IUserService;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiang
 * @since 2023-01-02
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private AllMoveTickeetService allTicketService;

    @Override
    public JsonResult selectMoveList() {
        JsonResult result = allTicketService.selectList();
        return result;
    }

    @Override
    public JsonResult buyMoveT(MovTicDto dto) {
        AssertUtil.notNull(dto.getTicketId(),"网络繁忙，有问题就是网络的原因");
        User user = selectById(1L);
        BigDecimal subtract = user.getPrice();

        dto.setBleance(subtract);

        JsonResult result = allTicketService.buyTick(dto);
        if (!result.isSuccess()){
            return result;
        }
        Double data = (Double)result.getData();

        user.setPrice(new BigDecimal(data));
        updateById(user);
        return JsonResult.success();
    }

    @Override
    public JsonResult watchMove(Long id) {
        AssertUtil.notNull(id,"网络繁忙，有问题就是网络的原因");
        JsonResult result = allTicketService.geta(id);

        return result;
    }

    @Override
    public JsonResult saveU(User user) {
        if(user==null){
            return JsonResult.error("user都是为空啊");
        }
        super.insert(user);

        JsonResult fuli = allTicketService.fuli(user.getId());
        return fuli;
    }

}
